package pl.edu.agh.thieves.cuckoo;

import pl.edu.agh.thieves.common.Item;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

public class Cuckoo {
	private final double capacity;
	private final ArrayList<Item> allItems;
	private final Random random;
	
	public Cuckoo(double capacity, Collection<Item> items){
		this.capacity=capacity;
		allItems=new ArrayList<Item>();
		allItems.addAll(items);
		random=new Random();
	}
	
	public Egg generateNewEgg(){
		ArrayList<Item> itemsForRandomization=new ArrayList<Item>();
		ArrayList<Item> itemsForEgg=new ArrayList<Item>();
		itemsForRandomization.addAll(allItems);
		double actualWeight=0;
		int actualSize=itemsForRandomization.size();
		int index;
		
		while(actualWeight<=capacity && actualSize!=0){
			index=random.nextInt(actualSize);
			actualSize--;
			actualWeight+=itemsForRandomization.get(index).getWeight();
			if(actualWeight<=capacity){
				itemsForEgg.add(itemsForRandomization.get(index));
				itemsForRandomization.remove(index);
			}
		}
		
		
		return new Egg(itemsForEgg);
	}
}
